home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Mindy / Mindy 1.2 - Mac PPC / demos / debug / string-extensions-test.dylan < prev    next >
Encoding:
Text File  |  1995-03-15  |  11.5 KB  |  314 lines  |  [TEXT/MPCC]

  1. module: regression-test
  2. author: Nick Kramer (nkramer@cs.cmu.edu)
  3. copyright:  Copyright (C) 1994, Carnegie Mellon University.
  4.             All rights reserved.
  5. synopsis: A regression test for the string-extensions library.
  6. rcs-header: $Header: string-extensions-test.dylan,v 1.6 94/11/09 22:41:44 nkramer Exp $
  7.  
  8. //======================================================================
  9. //
  10. // Copyright (c) 1994  Carnegie Mellon University
  11. // All rights reserved.
  12. // 
  13. // Use and copying of this software and preparation of derivative
  14. // works based on this software are permitted, including commercial
  15. // use, provided that the following conditions are observed:
  16. // 
  17. // 1. This copyright notice must be retained in full on any copies
  18. //    and on appropriate parts of any derivative works.
  19. // 2. Documentation (paper or online) accompanying any system that
  20. //    incorporates this software, or any part of it, must acknowledge
  21. //    the contribution of the Gwydion Project at Carnegie Mellon
  22. //    University.
  23. // 
  24. // This software is made available "as is".  Neither the authors nor
  25. // Carnegie Mellon University make any warranty about the software,
  26. // its performance, or its conformity to any specification.
  27. // 
  28. // Bug reports, questions, comments, and suggestions should be sent by
  29. // E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  30. //
  31. //======================================================================
  32.  
  33. define library regression-test
  34.   use dylan;
  35.   use string-extensions;
  36. end library regression-test;
  37.  
  38. define module regression-test
  39.   use dylan;
  40.   use extensions;            // need main
  41.   use regular-expressions;
  42.   use substring-search;
  43.   use string-hacking;
  44.   use cheap-io;
  45. end module regression-test;
  46.  
  47. define variable has-errors = #f;
  48.  
  49. define method main (argv0, #rest ignored)
  50.   format("Regression test for the string-extensions library.\n\n");
  51.   positioner-test();
  52.   replace-test();
  53.   translate-test();
  54.   split-test();
  55.   substring-search-test();
  56. //  case-insensitive-equal-test();   // Takes a really long time
  57.   join-test();
  58.   if (has-errors)
  59.     format("\n********* Warning!  Regression test failed! ***********\n");
  60.   else
  61.     format("All tests pass.\n");
  62.   end if;
  63. end method main;
  64.  
  65. define method run-test (input, expected-result, test-name :: <string>)
  66.  => passed? :: <boolean>;
  67.   if (input ~= expected-result)
  68.     has-errors := #t;
  69.     format("Failed %s\n", test-name);
  70.     format("     Got %=\n", input);
  71.     format("     when we expected %=\n", expected-result);
  72.     #f;
  73.   else
  74.     #t;
  75.   end if;
  76. end method run-test;
  77.  
  78. define method join-test ();
  79.   format("join\n");
  80.   run-test(join(", ", "dirty word", "clean word",
  81.         "computer word", "spanish word"),
  82.        "dirty word, clean word, computer word, spanish word",
  83.        "Join test 1");
  84.   run-test(join(", "), "", "Join test 2");
  85. end method join-test;
  86.  
  87.  
  88. // Try all 256x256 combinations to make sure our fancy definition of
  89. // case-insensitive-equal works.
  90. //
  91. define method case-insensitive-equal-test ()
  92.   format("Case insensitive equal\n");
  93.   for (c1 = as(<character>, 0) then successor(c1), 
  94.        until c1 = as(<character>, 255))
  95.     for (c2 = as(<character>, 0) then successor(c2), 
  96.      until c2 = as(<character>, 255))
  97.       run-test(case-insensitive-equal(c1, c2), 
  98.            as-lowercase(c1) = as-lowercase(c2),
  99.            "Case-insensitive equal");
  100.     end for;
  101.   end for;
  102. end method case-insensitive-equal-test;
  103.  
  104.  
  105. define method substring-search-test ()
  106.   let big-string = "The rain in spain and some other text";
  107.   run-test(substring-position(big-string, "spain"), 12, "substring-position");
  108.   let positioner = make-substring-positioner("spain");
  109.   run-test(positioner(big-string), 12, "make-substring-positioner");
  110.   run-test(substring-replace(big-string, "spain", "Pittsburgh"),
  111.        "The rain in Pittsburgh and some other text",
  112.        "substring-replace");
  113.   let replacer = make-substring-replacer("spain");
  114.   run-test(replacer(big-string, "Pittsburgh"),
  115.        "The rain in Pittsburgh and some other text",
  116.        "make-substring-replacer");   
  117. end method substring-search-test;
  118.  
  119.  
  120. define method replace-test ()
  121.   format("regexp-replace\n");
  122.   let big-string = "The rain in spain and some other text";
  123.   run-test(regexp-replace(big-string, "the (.*) in (\\w*\\b)",
  124.               "\\2 has it's \\1"),
  125.        "spain has it's rain and some other text",
  126.        "regexp-replace #1");
  127.   run-test(regexp-replace(big-string, "in", "out"),
  128.        "The raout out spaout and some other text",
  129.        "regexp-replace #2");
  130.   run-test(regexp-replace(big-string, "in", "out", count: 2),
  131.        "The raout out spain and some other text",
  132.        "regexp-replace #3");
  133.   run-test(regexp-replace(big-string, "in", "out", start: 8, end: 15),
  134.        "The rain out spain and some other text",
  135.        "regexp-replace #4");
  136.  
  137.   format("Make-regexp-replacer\n");
  138.   let replacer1 = make-regexp-replacer("the (.*) in (\\w*\\b)");
  139.   run-test(replacer1(big-string, "\\2 has it's \\1"),
  140.        "spain has it's rain and some other text",
  141.        "make-regexp-replacer #1");
  142.   let replacer2 = make-regexp-replacer("the (.*) in ([\\w]*\\b)", 
  143.                        replace-with: "\\2 has it's \\1");
  144.   run-test(replacer2(big-string),
  145.        "spain has it's rain and some other text",
  146.        "make-regexp-replacer #2");
  147. end method replace-test;
  148.  
  149.  
  150. define method translate-test ()
  151.   let big-string = "The rain in spain and some other text";
  152.   format("translate\n");
  153.   run-test(translate(big-string, "a-z", "A-Z"),
  154.        "THE RAIN IN SPAIN AND SOME OTHER TEXT",
  155.        "translate test 1");
  156.   run-test(translate(big-string, "a-zA-Z", "A-Za-z"),
  157.        "tHE RAIN IN SPAIN AND SOME OTHER TEXT",
  158.        "translate test 2");
  159.   run-test(translate(big-string, "a-z ", "", delete: #t),
  160.        "T", 
  161.        "translate test 3");
  162.   run-test(translate(big-string, "a-z", "A-Z", start: 5, end: 10),
  163.        "The rAIN In spain and some other text",
  164.        "translate test 4");
  165.   
  166.   format("make-translator\n");
  167.   let translator1 = make-translator("a-z", "A-Z");
  168.   run-test(translator1(big-string),
  169.        "THE RAIN IN SPAIN AND SOME OTHER TEXT",
  170.        "make-translator #1");
  171.   let translator2 = make-translator("a-zA-Z", "A-Za-z");
  172.   run-test(translator2(big-string),
  173.        "tHE RAIN IN SPAIN AND SOME OTHER TEXT",
  174.        "make-translator #2");
  175.   let translator3 = make-translator("a-z ", "", delete: #t);
  176.   run-test(translator3(big-string),
  177.        "T",
  178.        "make-translator #3");
  179.   let translator4 = make-translator("a-z", "A-Z");
  180.   run-test(translator4(big-string, start: 5, end: 10),
  181.        "The rAIN In spain and some other text",
  182.        "make-translator #4");
  183. end method translate-test;
  184.  
  185.  
  186. define method split-test ()
  187.   let big-string = "The rain in spain and some other text";
  188.   block ()
  189.     format("split\n");
  190.     let (#rest test1) = split("\\s", big-string);
  191.     run-test(test1, 
  192.          #("The", "rain", "in", "spain", "and", "some", "other", "text"),
  193.          "split #1");
  194.     let (#rest test2) = split("\\s", big-string, count: 3);
  195.     run-test(test2, #("The", "rain", "in spain and some other text"),
  196.          "split #2");
  197.     let (#rest test3) = split("\\s", big-string, start: 12);
  198.     run-test(test3, #("The rain in spain", "and", "some", "other", "text"),
  199.          "split #3");
  200.     let (#rest test4) = split("\\s", " Some   text with   lots of spaces  ", 
  201.                   count: 3);
  202.     run-test(test4, #("Some", "text", "with   lots of spaces  "),
  203.          "split #4");
  204.     let (#rest test5) = split("\\s", " Some   text with   lots of spaces  ", 
  205.                   count: 3, remove-empty-items: #f);
  206.     run-test(test5, #("", "Some", "  text with   lots of spaces  "),
  207.          "split #5");
  208.   end block;
  209.  
  210.   block ()
  211.     format("make-splitter\n");
  212.     let splitter1 = make-splitter("\\s");
  213.     let (#rest test1) = splitter1(big-string);
  214.     run-test(test1, 
  215.          #("The", "rain", "in", "spain", "and", "some", "other", "text"),
  216.          "make-splitter #1");
  217.     let splitter2 = make-splitter("\\s");
  218.     let (#rest test2) = splitter2(big-string, count: 3);
  219.     run-test(test2, #("The", "rain", "in spain and some other text"),
  220.          "make-splitter #2");
  221.     let splitter3 = make-splitter("\\s");
  222.     let (#rest test3) = splitter3(big-string, start: 12);
  223.     run-test(test3, #("The rain in spain", "and", "some", "other", "text"),
  224.          "make-splitter #3");
  225.     let splitter4 = make-splitter("\\s");
  226.     let (#rest test4) = splitter4(" Some   text with   lots of spaces  ", 
  227.                   count: 3);
  228.     run-test(test4, #("Some", "text", "with   lots of spaces  "),
  229.          "make-splitter #4");
  230.     let splitter5 = make-splitter("\\s");
  231.     let (#rest test5) = splitter5(" Some   text with   lots of spaces  ", 
  232.                   count: 3, remove-empty-items: #f);
  233.     run-test(test5, #("", "Some", "  text with   lots of spaces  "),
  234.          "make-splitter #5");
  235.   end block;
  236. end method split-test;
  237.  
  238. define method positioner-test ();
  239.   format("regexp-positioner\n");
  240.   test-regexp("a*", "aaaaaaaaaa", #[0, 10]);
  241.   test-regexp("a*", "aaaaabaaaa", #[0, 5]);
  242.   test-regexp("ab*(cd|e)", "acd", #[0, 3, 1, 3]);
  243.   test-regexp("ab*(cd|e)", "abbbbe", #[0, 6, 5, 6]);
  244.   test-regexp("ab*(cd|e)", "ab", #f);
  245.     
  246.   test-regexp("^a$", "aaaaaaaaaaaaaa", #f);
  247.   test-regexp("^a$", "a", #[0, 1]);
  248.   test-regexp("(^a$)|aba", "abba", #f);
  249.   test-regexp("(^a$)|aba", "aba", #[0, 3, #f, #f]);
  250.  
  251.   test-regexp("\\bthe rain (in){1,5} spain$", "the rain in spain", 
  252.           #[0, 17, 9, 11]);
  253.   test-regexp("\\bthe rain (in){1,5} spain$", "the rain spain",
  254.           #f);
  255.   test-regexp("\\bthe rain (in){1,5} spain$", "the rain ininin spain",
  256.           #[0, 21, 13, 15]);
  257.   test-regexp("\\bthe rain (in){1,5} spain$", "bork the rain in spain",
  258.           #[5, 22, 14, 16]);
  259.   test-regexp("\\bthe rain (in){1,5} spain$", "the rain in spainland", #f);
  260.   test-regexp("\\bthe rain (in){1,5} spain$", "blathe rain in spain", #f);
  261.   test-regexp("\\bthe rain (in){1,5} spain$", "the rain ininininin spain",
  262.           #[0, 25, 17, 19]);
  263.   test-regexp("\\bthe rain (in){1,5} spain$", "the rain inininininin spain",
  264.           #f);
  265.   test-regexp("((a*)|(b*))*c", "aaabbabbacbork", 
  266.           #[0, 10, 8, 9, 8, 9, 6, 8]);
  267.                    // The real test is whether or not this terminates
  268.   test-regexp("a*", "aaaaa", #[0, 5]);
  269.   test-regexp("a*", "a", #[0, 1]);
  270.   test-regexp("a*", "", #[0, 0]);
  271.   test-regexp("bba*c", "bbc", #[0, 3]);
  272.   test-regexp("a", "bbbb", #f);
  273.   test-regexp("a*", "aaaaa", #[3, 4], start: 3, end: 4);
  274.   test-regexp("^a*", "aaaaa", #[2, 5], start: 2);
  275.   test-regexp("^a*", "baaaaa", #[2, 6], start: 2);
  276.   test-regexp("^a+", "bbbaaaaa", #f, start: 2);
  277.   test-regexp("a+", "AAaAA", #[0, 5]);
  278.   test-regexp("a+", "AAaAA", #[2, 3], case-sensitive: #t);
  279.   test-regexp("[a-f]+", "SdFbIeNvI", #[1, 4]);
  280.   test-regexp("[a-f]+", "SdFbIeNvI", #[1, 2], case-sensitive: #t);
  281. end method positioner-test;
  282.  
  283.  
  284. define method test-regexp(regexp :: <string>, input :: <string>,
  285.               right-marks, #key output: output = #t,
  286.               start: start, end: input-end = #f,
  287.               case-sensitive: case? = #f)
  288.     => passed? :: <boolean>;
  289.   let (#rest marks) = if (~start & ~input-end) 
  290.             regexp-position(input, regexp, case-sensitive: case?);
  291.               elseif (start & input-end)
  292.             regexp-position(input, regexp, case-sensitive: case?, 
  293.                     start: start, end: input-end);
  294.               elseif (start)
  295.             regexp-position(input, regexp, case-sensitive: case?,
  296.                     start: start);
  297.               else
  298.             regexp-position(input, regexp, case-sensitive: case?,
  299.                     end: input-end);
  300.               end if;
  301.   let answer = marks[0];
  302.   if (marks = right-marks | answer = right-marks)
  303.     #f;   // do nothing
  304.   elseif (answer ~= #f & right-marks ~= #f)
  305.     format("Failed marks: regexp-position on\n");
  306.     format("     regexp=%=, big=%=\n", regexp, input);
  307.     format("     returned %=\n", marks);
  308.   else
  309.     format("Failed both: regexp-position on\n");
  310.     format("     regexp=%=, big=%=\n", regexp, input);
  311.     format("     returned %=\n", marks);
  312.   end if;
  313. end method test-regexp;
  314.